package cc.orange.adapter;

import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import cc.orange.R;
import cc.orange.entity.KFChatListEntity;
import cc.orange.utils.CircleImageView;
import cc.orange.utils.emojis.AndroidEmoji;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;


/**
 * Created by CM on 2022/10/25.
 */

public class MsgCon4KFAdapter extends BaseQuickAdapter<KFChatListEntity.Data, BaseViewHolder> {
    private int types;
    public MsgCon4KFAdapter(@Nullable List<KFChatListEntity.Data> data,int types) {
        super(R.layout.rc_message_item, data);
        this.types = types;
    }

    private String name,photo,uid;
    public void setNamePhoto(String name,String photo,String uid){
        this.name = name;
        this.photo = photo;
        this.uid = uid;
    }
    @Override
    protected void convert(@NonNull BaseViewHolder helper, KFChatListEntity.Data item) {

        CircleImageView rc_left_portrait = helper.getView(R.id.rc_left_portrait);
        CircleImageView rc_right_portrait = helper.getView(R.id.rc_right_portrait);
        RelativeLayout rc_content = helper.getView(R.id.rc_content);//
        TextView  talk_msg_content = helper.getView(R.id.talk_msg_content); //消息文本
        TextView  rc_time = helper.getView(R.id.rc_time); //时间

        ConstraintLayout rc_cl_content = helper.getView(R.id.rc_cl_content);
        RelativeLayout item_congv_rel2 = helper.getView(R.id.item_congv_rel2);//布局2 提示文字
        RelativeLayout item_congv_rel3 = helper.getView(R.id.item_congv_rel3);//布局3 带头像提示文字
        if(types == 3){
            item_congv_rel3.setVisibility(View.VISIBLE);
            item_congv_rel2.setVisibility(View.GONE);
            rc_cl_content.setVisibility(View.GONE);
        }else{
        rc_cl_content.setVisibility(View.VISIBLE);
            item_congv_rel3.setVisibility(View.GONE);
            item_congv_rel2.setVisibility(View.GONE);

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(0,20,0,0);
            talk_msg_content.setText("");
            if(item.getClassify() == 1){//用户  1客服
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                rc_content.setBackgroundResource(R.drawable.shape_msg_left);
                talk_msg_content.setTextColor(mContext.getResources().getColor(R.color.black));
                rc_left_portrait.setVisibility(View.VISIBLE);
                rc_right_portrait.setVisibility(View.GONE);
                RequestOptions qe = new RequestOptions();
                if(!TextUtils.isEmpty(item.getPhoto())) {
                    qe.error(item.getPhoto().equals("img_1") ?
                            R.drawable.icon_login_img3 : R.drawable.icon_login_img4);
                }else{
                    qe.error(R.drawable.icon_login_img3);
                }
                Glide.with(mContext).load(item.getPhoto()).apply(qe).into(rc_left_portrait);
            }else{
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                rc_content.setBackgroundResource(R.drawable.shape_msg_right);
                talk_msg_content.setTextColor(mContext.getResources().getColor(R.color.white));
                rc_left_portrait.setVisibility(View.GONE);
                rc_right_portrait.setVisibility(View.VISIBLE);
                ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
                String url_hr = ss.getString("url_hr", "");
                RequestOptions qe = new RequestOptions();
                if(!TextUtils.isEmpty(item.getPhoto())) {
                    qe.error(item.getPhoto().equals("img_1") ?
                            R.drawable.icon_login_img3 : R.drawable.icon_login_img4);
                }else{
                    qe.error(R.drawable.icon_login_img3);
                }
                Glide.with(mContext).load(url_hr).apply(qe).into(rc_right_portrait);
            }
            rc_content.setLayoutParams(layoutParams);


            String content = item.getText();
            content = content.replace("\n", " ");
            if (content.length() > 100) {
                content = content.substring(0, 100);
            }
            talk_msg_content.setText(new SpannableString(AndroidEmoji.ensure(content)));
        }
    }
}
